/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.wdev.dbdevassist.action.base;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.TableItem;

import top.wardyan.base.model.BaseModel;
import top.wardyan.conf.Const;
import top.wardyan.conf.config.SQLite;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WTable;
import top.wardyan.control.WTableItem;
import top.wardyan.control.WText;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.YTObject;
import top.wardyan.wdev.dbdevassist.model.YTObjectCol;
import top.wardyan.wdev.dbdevassist.model.YTObjectPara;
import top.wardyan.wdev.dbdevassist.service.IDetailService;

/**
 * 有两个明细表的
 * @author ward 2020-05-16
 */
public abstract class BaseMasterDetailsEditShell<T extends BaseMasterDetailsEditShell<T>>
										extends BaseMasterEditShell<T, YTObject>{

	protected WComposite mainCom;
	protected IDetailService<YTObjectPara> paraService;
	protected IDetailService<YTObjectCol> colService;
	protected WComposite paraCom;
	protected WButton btnParaAdd;
	protected WButton btnParaDel;
	protected WButton btnParaClear;
	protected WButton btnParaRefresh;
	protected WButton btnParaGet;	// 获取
	protected WButton btnParaAutoname;	// 获取
	protected WComposite paraTopCom;
	
	protected ScrolledComposite comParaTable;
	protected WTable tblParaTable;
	
	
	protected WComposite colCom;
	protected WButton btnColAdd;
	protected WButton btnColDel;
	protected WButton btnColClear;
	protected WButton btnColRefresh;
	protected WButton btnColGet;	// 获取
	protected WButton btnColAutoname;	// 获取
	protected WComposite colTopCom;
	
	protected ScrolledComposite comColTable;
	protected WTable tblColTable;
	
	
	
	protected CTabFolder tabfolder;
	protected CTabItem paraItem;
	protected CTabItem colItem;
	protected String xName;
	
	int pad = 14;
	
	protected int type;	// bit位标记 1para 2col
	
	

	protected WText txtXName;
	protected WText txtXHint;
	protected WText txtXDesc;
	protected WText txtXNote;
	
	
	
	public BaseMasterDetailsEditShell(int sID, String xName, int type) {
		super(sID, DDA.getInstance().objectService);
		this.xName = xName;
		this.type = type;
		this.paraService = dda.objectParaService;
		this.colService = dda.objectColService;
		this.createContents();

		this.txtXHint.setFocus();
	}
	

	private void createContents() {
		// detail
		mainCom = new WComposite(this, SWT.BORDER);
		mainCom.setLayout(new FormLayout());
		//mainCom.setLayout(layout);
		FormData fd_mainCom = new FormData();
		fd_mainCom.left = new FormAttachment(0);
		fd_mainCom.top = new FormAttachment(topCom, 5);
		fd_mainCom.right = new FormAttachment(100, -1);
		//fd_mainCom.bottom = new FormAttachment(mainCom, 45);
		mainCom.setLayoutData(fd_mainCom);
		
		
		tabfolder = new CTabFolder(this, SWT.BORDER);
		tabfolder.setLayout(new FormLayout());
		FormData fd_tabfolder = new FormData();
		fd_tabfolder.left = new FormAttachment(0);
		fd_tabfolder.top = new FormAttachment(mainCom, 5);
		fd_tabfolder.right = new FormAttachment(100, -1);
		fd_tabfolder.bottom = new FormAttachment(100, -1);
		tabfolder.setLayoutData(fd_tabfolder);
		
		paraItem = new CTabItem(tabfolder, SWT.NONE);
		paraCom = new WComposite(tabfolder, SWT.BORDER);
		paraCom.setLayout(new FormLayout());
		FormData fd_paraCom = new FormData();
		fd_paraCom.left = new FormAttachment(0);
		fd_paraCom.top = new FormAttachment(mainCom, 5);
		fd_paraCom.right = new FormAttachment(100, -1);
		fd_paraCom.bottom = new FormAttachment(100, -1);
		paraCom.setLayoutData(fd_paraCom);
		//paraItem.setControl(paraCom);
		paraItem.setControl(paraCom);
		paraItem.setText("    参数    ");
		
		paraTopCom = new WComposite(paraCom, SWT.BORDER);
		paraTopCom.setLayout(new FormLayout());
		FormData fd_paraTopCom = new FormData();
		fd_paraTopCom.left = new FormAttachment(0);
		fd_paraTopCom.top = new FormAttachment(paraCom, 2);
		fd_paraTopCom.right = new FormAttachment(100, -1);
		fd_paraTopCom.bottom = new FormAttachment(paraTopCom, 40);
		paraTopCom.setLayoutData(fd_paraTopCom);
		
		btnParaAdd = new WButton(paraTopCom, SWT.PUSH);
		btnParaAdd.setText("新增");
		FormData fd_btnParaAdd = new FormData();
		fd_btnParaAdd.left = new FormAttachment(paraTopCom, 10);
		fd_btnParaAdd.top = new FormAttachment(paraTopCom, 2);
		fd_btnParaAdd.right = new FormAttachment(btnParaAdd, pad * 4);
		fd_btnParaAdd.bottom = new FormAttachment(btnParaAdd, 30);
		btnParaAdd.setLayoutData(fd_btnParaAdd);
		btnParaAdd.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				self.getParaEditShell(self.sID, 0);
				self.refreshParas();
			}
			
		});
		
		btnParaDel = new WButton(paraTopCom, SWT.PUSH);
		btnParaDel.setText("删除");
		FormData fd_btnParaDel = new FormData();
		fd_btnParaDel.left = new FormAttachment(btnParaAdd, 10);
		fd_btnParaDel.top = new FormAttachment(paraTopCom, 2);
		fd_btnParaDel.right = new FormAttachment(btnParaDel, pad * 4);
		fd_btnParaDel.bottom = new FormAttachment(btnParaDel, 30);
		btnParaDel.setLayoutData(fd_btnParaDel);
		btnParaDel.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = self.tblParaTable.getSelectionIndex();
				if(index >= 0) {
					WTableItem item = (WTableItem)self.tblParaTable.getItem(index);
					//top.wardyan.conf.model.YTTableCol tc = SQLite.getInstance(DDA.SQLTEMPLATE).tablecolService.find(self.getClass().getName(), "tblParaTable", "dID");
					BaseModel<?> model = item.getModel();
					if(model != null) {
						//int dID = Integer.valueOf(item.getText(tc.getSIndex()));
						if(Msg.confirm(self, "警告", "确定删除吗?注意,删除操作不可恢复", SWT.OK | SWT.CANCEL) == SWT.OK) {
							//paraService.deleteByIDs(self.sID, dID);
							paraService.delete(model);
							self.refreshParas();
						}
						
					}
				}
			}
			
		});
		
		btnParaClear = new WButton(paraTopCom, SWT.PUSH);
		btnParaClear.setText("清空");
		FormData fd_btnParaClear = new FormData();
		fd_btnParaClear.left = new FormAttachment(btnParaDel, 10);
		fd_btnParaClear.top = new FormAttachment(paraTopCom, 2);
		fd_btnParaClear.right = new FormAttachment(btnParaClear, pad * 4);
		fd_btnParaClear.bottom = new FormAttachment(btnParaClear, 30);
		btnParaClear.setLayoutData(fd_btnParaClear);
		
		btnParaClear.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if(self.tblParaTable.getItems().length > 0 && Msg.confirm(self, "警告", "确定清空吗?注意,清空操作不可恢复", SWT.OK | SWT.CANCEL) == SWT.OK) {
					List<YTObjectPara> dis = paraService.list(self.sID);
					if(dis != null) {
						for(YTObjectPara d : dis)
							paraService.delete(d);
						self.refreshParas();
					}
				}
			}
			
		});

		btnParaRefresh = new WButton(paraTopCom, SWT.PUSH);
		btnParaRefresh.setText("刷新");
		FormData fd_btnParaRefresh = new FormData();
		fd_btnParaRefresh.left = new FormAttachment(btnParaClear, 10);
		fd_btnParaRefresh.top = new FormAttachment(paraTopCom, 2);
		fd_btnParaRefresh.right = new FormAttachment(btnParaRefresh, pad * 4);
		fd_btnParaRefresh.bottom = new FormAttachment(btnParaRefresh, 30);
		btnParaRefresh.setLayoutData(fd_btnParaRefresh);
		btnParaRefresh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// self.tblParaTable.show("tblParaTable", "", self.paraService.list(self.sID));
				self.refreshParas();
			}
		});
		
		btnParaGet = new WButton(paraTopCom, SWT.PUSH);
		btnParaGet.setText("获取");
		FormData fd_btnParaGet = new FormData();
		fd_btnParaGet.left = new FormAttachment(btnParaRefresh, 10);
		fd_btnParaGet.top = new FormAttachment(paraTopCom, 2);
		fd_btnParaGet.right = new FormAttachment(btnParaGet, pad * 4);
		fd_btnParaGet.bottom = new FormAttachment(btnParaGet, 30);
		btnParaGet.setLayoutData(fd_btnParaGet);
		btnParaGet.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.getParas();
			}
		});
		
		btnParaAutoname = new WButton(paraTopCom, SWT.PUSH);
		btnParaAutoname.setText("自动命名");
		FormData fd_btnParaAutoname = new FormData();
		fd_btnParaAutoname.left = new FormAttachment(btnParaGet, 10);
		fd_btnParaAutoname.top = new FormAttachment(paraTopCom, 2);
		fd_btnParaAutoname.right = new FormAttachment(btnParaAutoname, pad * 6);
		fd_btnParaAutoname.bottom = new FormAttachment(btnParaAutoname, 30);
		btnParaAutoname.setLayoutData(fd_btnParaAutoname);
		btnParaAutoname.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//self.getParas();
			}
		});
		
		//
		//comParaTable = new WComposite(paraCom, SWT.BORDER);
		comParaTable = new ScrolledComposite(paraCom, SWT.BORDER);
		comParaTable.setLayout(new FormLayout());
		FormData fd_comParaTable = new FormData();
		fd_comParaTable.left = new FormAttachment(0);
		fd_comParaTable.top = new FormAttachment(paraTopCom, 2);
		fd_comParaTable.right = new FormAttachment(100, -1);
		fd_comParaTable.bottom = new FormAttachment(comParaTable, 300);
		comParaTable.setLayoutData(fd_comParaTable);
		
		tblParaTable = new WTable(comParaTable, SWT.NONE, "tblParaTable", self.paraService, dda.dicService);
		FormData fd_tblParaTable = new FormData();
		fd_tblParaTable.left = new FormAttachment(0);
		fd_tblParaTable.top = new FormAttachment(comParaTable, 2);
		tblParaTable.setLayoutData(fd_tblParaTable);
		//self.refreshParas();
		//tblParaTable.setSize(self.tblParaTable.getSize().x, 500);
		tblParaTable.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(self.model != null && self.model.getSStatus().getSID() == 2) {
					int index = tblParaTable.getSelectionIndex();
					if(index >= 0) {
						WTableItem item = (WTableItem)self.tblParaTable.getItem(index);
						BaseModel<?> model = null;
						if(item != null && (model = item.getModel()) != null) {
							int dID = model.getInt("dID");
							var s = self.getParaEditShell(self.sID, dID);
							if(s != null) {
								s.go();
								self.refreshParas();
							}
						}
					}
				}
			}
			
		});
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		colItem = new CTabItem(tabfolder, SWT.NONE);
		colCom = new WComposite(tabfolder, SWT.BORDER);
		colCom.setLayout(new FormLayout());
		FormData fd_colCom = new FormData();
		fd_colCom.left = new FormAttachment(0);
		fd_colCom.top = new FormAttachment(mainCom, 5);
		fd_colCom.right = new FormAttachment(100, -1);
		fd_colCom.bottom = new FormAttachment(100, -1);
		colCom.setLayoutData(fd_colCom);
		//colItem.setControl(colCom);
		colItem.setControl(colCom);
		colItem.setText("    数据集    ");
		
		colTopCom = new WComposite(colCom, SWT.BORDER);
		colTopCom.setLayout(new FormLayout());
		FormData fd_colTopCom = new FormData();
		fd_colTopCom.left = new FormAttachment(0);
		fd_colTopCom.top = new FormAttachment(colCom, 2);
		fd_colTopCom.right = new FormAttachment(100, -1);
		fd_colTopCom.bottom = new FormAttachment(colTopCom, 40);
		colTopCom.setLayoutData(fd_colTopCom);
		
		btnColAdd = new WButton(colTopCom, SWT.PUSH);
		btnColAdd.setText("新增");
		FormData fd_btnColAdd = new FormData();
		fd_btnColAdd.left = new FormAttachment(colTopCom, 10);
		fd_btnColAdd.top = new FormAttachment(colTopCom, 2);
		fd_btnColAdd.right = new FormAttachment(btnColAdd, pad * 4);
		fd_btnColAdd.bottom = new FormAttachment(btnColAdd, 30);
		btnColAdd.setLayoutData(fd_btnColAdd);
		btnColAdd.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				self.getColEditShell(self.sID, 0);
				self.refreshCols();
			}
			
		});
		
		btnColDel = new WButton(colTopCom, SWT.PUSH);
		btnColDel.setText("删除");
		FormData fd_btnColDel = new FormData();
		fd_btnColDel.left = new FormAttachment(btnColAdd, 10);
		fd_btnColDel.top = new FormAttachment(colTopCom, 2);
		fd_btnColDel.right = new FormAttachment(btnColDel, pad * 4);
		fd_btnColDel.bottom = new FormAttachment(btnColDel, 30);
		btnColDel.setLayoutData(fd_btnColDel);
		btnColDel.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = self.tblColTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = self.tblColTable.getItem(index);
					top.wardyan.conf.model.YTTableCol tc = SQLite.getInstance(DDA.SQLTEMPLATE).tablecolService.find(self.getClass().getName(), "tblColTable", "dID");
					if(tc != null) {
						int dID = Integer.valueOf(item.getText(tc.getSIndex()));
						if(Msg.confirm(self, "警告", "确定删除吗?注意,删除操作不可恢复", SWT.OK | SWT.CANCEL) == SWT.OK) {
							colService.deleteByIDs(self.sID, dID);
							self.refreshCols();
						}
						
					}
				}
			}
			
		});
		
		btnColClear = new WButton(colTopCom, SWT.PUSH);
		btnColClear.setText("清空");
		FormData fd_btnColClear = new FormData();
		fd_btnColClear.left = new FormAttachment(btnColDel, 10);
		fd_btnColClear.top = new FormAttachment(colTopCom, 2);
		fd_btnColClear.right = new FormAttachment(btnColClear, pad * 4);
		fd_btnColClear.bottom = new FormAttachment(btnColClear, 30);
		btnColClear.setLayoutData(fd_btnColClear);
		
		btnColClear.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if(self.tblColTable.getItems().length > 0 && Msg.confirm(self, "警告", "确定清空吗?注意,清空操作不可恢复", SWT.OK | SWT.CANCEL) == SWT.OK) {
					List<YTObjectCol> dis = colService.list(self.sID);
					if(dis != null) {
						for(YTObjectCol d : dis)
							colService.delete(d);
						self.refreshCols();
					}
				}
			}
			
		});

		btnColRefresh = new WButton(colTopCom, SWT.PUSH);
		btnColRefresh.setText("刷新");
		FormData fd_btnColRefresh = new FormData();
		fd_btnColRefresh.left = new FormAttachment(btnColClear, 10);
		fd_btnColRefresh.top = new FormAttachment(colTopCom, 2);
		fd_btnColRefresh.right = new FormAttachment(btnColRefresh, pad * 4);
		fd_btnColRefresh.bottom = new FormAttachment(btnColRefresh, 30);
		btnColRefresh.setLayoutData(fd_btnColRefresh);
		btnColRefresh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// self.tblColTable.show("tblColTable", "", self.colService.list(self.sID));
				self.refreshCols();
			}
		});
		
		btnColGet = new WButton(colTopCom, SWT.PUSH);
		btnColGet.setText("获取");
		FormData fd_btnColGet = new FormData();
		fd_btnColGet.left = new FormAttachment(btnColRefresh, 10);
		fd_btnColGet.top = new FormAttachment(colTopCom, 2);
		fd_btnColGet.right = new FormAttachment(btnColGet, pad * 4);
		fd_btnColGet.bottom = new FormAttachment(btnColGet, 30);
		btnColGet.setLayoutData(fd_btnColGet);
		btnColGet.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.getCols();
			}
		});
		
		btnColAutoname = new WButton(colTopCom, SWT.PUSH);
		btnColAutoname.setText("自动命名");
		FormData fd_btnColAutoname = new FormData();
		fd_btnColAutoname.left = new FormAttachment(btnColGet, 10);
		fd_btnColAutoname.top = new FormAttachment(colTopCom, 2);
		fd_btnColAutoname.right = new FormAttachment(btnColAutoname, pad * 6);
		fd_btnColAutoname.bottom = new FormAttachment(btnColAutoname, 30);
		btnColAutoname.setLayoutData(fd_btnColAutoname);
		btnColAutoname.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//self.getCols();
			}
		});
		
		
		
		//
		//comColTable = new WComposite(colCom, SWT.BORDER);
		comColTable = new ScrolledComposite(colCom, SWT.BORDER);
		comColTable.setLayout(new FormLayout());
		FormData fd_comColTable = new FormData();
		fd_comColTable.left = new FormAttachment(0);
		fd_comColTable.top = new FormAttachment(colTopCom, 2);
		fd_comColTable.right = new FormAttachment(100, -1);
		fd_comColTable.bottom = new FormAttachment(comColTable, 300);
		comColTable.setLayoutData(fd_comColTable);
		
		tblColTable = new WTable(comColTable, SWT.NONE, "tblColTable", self.colService, dda.dicService);
		FormData fd_tblColTable = new FormData();
		fd_tblColTable.left = new FormAttachment(0);
		fd_tblColTable.top = new FormAttachment(comColTable, 2);
		tblColTable.setLayoutData(fd_tblColTable);
		self.refreshCols();
		
		tblColTable.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(self.model != null && self.model.getSStatus().getSID() == 2) {
					int index = tblColTable.getSelectionIndex();
					if(index >= 0) {
						WTableItem item = (WTableItem)self.tblColTable.getItem(index);
						BaseModel<?> model = null;
						if(item != null && (model = item.getModel()) != null) {
							int dID = model.getInt("dID");
							var s = self.getColEditShell(self.sID, dID);
							if(s != null) {
								s.go();
								self.refreshCols();
							}
						}
					}
				}
			}
			
		});
	
		//tabfolder.pack();
		int ok = this.type & 1;
		tabfolder.setSelection(ok > 0 ? paraItem : colItem);
		
		
		WComposite comXName = new WComposite(mainCom, SWT.NONE);
		comXName.setLayout(new FormLayout());
		FormData fd_comXName = new FormData();
		fd_comXName.left = new FormAttachment(mainCom, 4);
		fd_comXName.top = new FormAttachment(mainCom, 0);
		fd_comXName.right = new FormAttachment(45, 0);
		fd_comXName.bottom = new FormAttachment(comXName, 35);
		comXName.setLayoutData(fd_comXName);
		
		WLabel lblXName = new WLabel(comXName, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXName = new FormData();
		fd_lblXName.left = new FormAttachment(comXName, 0);
		fd_lblXName.top = new FormAttachment(comXName, 2);
		fd_lblXName.right = new FormAttachment(30);
		fd_lblXName.bottom = new FormAttachment(lblXName, 30);
		lblXName.setLayoutData(fd_lblXName);
		txtXName = new WText(comXName, SWT.BORDER, "", "名称");
		txtXName.setTextLimit(128);
		
		//txtXName.setEnabled(false);
		FormData fd_txtXName = new FormData();
		fd_txtXName.left = new FormAttachment(lblXName, 4);
		fd_txtXName.top = new FormAttachment(comXName, 2);
		fd_txtXName.right = new FormAttachment(100, -8);
		fd_txtXName.bottom = new FormAttachment(txtXName, 30);
		txtXName.setLayoutData(fd_txtXName);
		txtXName.setEditable(false);
		txtXName.setToolTipText("只读,不可更新");
		//txtXName.addKeyListener(new WReadonlyKeyAdapter());
//		txtXName.setEnabled(false);
//		txtXName.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseDown(MouseEvent e) {
//				WTool.setSysClipboardText(txtXName.getText());
//				Msg.show("复制成功", "已将名称复制到剪贴板");
//			}
//		});
		
		WComposite comXHint = new WComposite(mainCom, SWT.NONE);
		comXHint.setLayout(new FormLayout());
		FormData fd_comXHint = new FormData();
		fd_comXHint.left = new FormAttachment(comXName, 4);
		fd_comXHint.top = new FormAttachment(mainCom, 0);
		fd_comXHint.right = new FormAttachment(100, -2);
		fd_comXHint.bottom = new FormAttachment(comXHint, 35);
		comXHint.setLayoutData(fd_comXHint);
		WLabel lblXHint = new WLabel(comXHint, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXHint = new FormData();
		fd_lblXHint.left = new FormAttachment(comXHint, 0);
		fd_lblXHint.top = new FormAttachment(comXHint, 2);
		fd_lblXHint.right = new FormAttachment(30);
		fd_lblXHint.bottom = new FormAttachment(lblXHint, 30);
		lblXHint.setLayoutData(fd_lblXHint);
		txtXHint = new WText(comXHint, SWT.BORDER, "", "名称");
		txtXHint.setTextLimit(128);
		FormData fd_txtXHint = new FormData();
		fd_txtXHint.left = new FormAttachment(lblXHint, 4);
		fd_txtXHint.top = new FormAttachment(comXHint, 2);
		fd_txtXHint.right = new FormAttachment(100, -8);
		fd_txtXHint.bottom = new FormAttachment(txtXHint, 30);
		txtXHint.setLayoutData(fd_txtXHint);
		
		WComposite comXDesc = new WComposite(mainCom, SWT.NONE);
		comXDesc.setLayout(new FormLayout());
		FormData fd_comXDesc = new FormData();
		fd_comXDesc.left = new FormAttachment(mainCom, 4);
		fd_comXDesc.top = new FormAttachment(comXName, 2);
		fd_comXDesc.right = new FormAttachment(45, 0);
		fd_comXDesc.bottom = new FormAttachment(comXDesc, 35);
		comXDesc.setLayoutData(fd_comXDesc);
		WLabel lblXDesc = new WLabel(comXDesc, SWT.BORDER, Const.FONT16, "描述", SWT.RIGHT);
		FormData fd_lblXDesc = new FormData();
		fd_lblXDesc.left = new FormAttachment(comXDesc, 0);
		fd_lblXDesc.top = new FormAttachment(comXDesc, 2);
		fd_lblXDesc.right = new FormAttachment(30);
		fd_lblXDesc.bottom = new FormAttachment(lblXDesc, 30);
		lblXDesc.setLayoutData(fd_lblXDesc);
		txtXDesc = new WText(comXDesc, SWT.BORDER, "", "描述");
		txtXDesc.setTextLimit(256);
		FormData fd_txtXDesc = new FormData();
		fd_txtXDesc.left = new FormAttachment(lblXDesc, 4);
		fd_txtXDesc.top = new FormAttachment(comXDesc, 2);
		fd_txtXDesc.right = new FormAttachment(100, -8);
		fd_txtXDesc.bottom = new FormAttachment(txtXDesc, 30);
		txtXDesc.setLayoutData(fd_txtXDesc);
		
		
		WComposite comXNote = new WComposite(mainCom, SWT.NONE);
		comXNote.setLayout(new FormLayout());
		FormData fd_comXNote = new FormData();
		fd_comXNote.left = new FormAttachment(comXDesc, 4);
		fd_comXNote.top = new FormAttachment(comXName, 2);
		fd_comXNote.right = new FormAttachment(100, -1);
		fd_comXNote.bottom = new FormAttachment(comXNote, 35);
		comXNote.setLayoutData(fd_comXNote);
		WLabel lblXNote = new WLabel(comXNote, SWT.BORDER, Const.FONT16, "备注", SWT.RIGHT);
		FormData fd_lblXNote = new FormData();
		fd_lblXNote.left = new FormAttachment(comXNote, 0);
		fd_lblXNote.top = new FormAttachment(comXNote, 2);
		fd_lblXNote.right = new FormAttachment(30);
		fd_lblXNote.bottom = new FormAttachment(lblXNote, 30);
		lblXNote.setLayoutData(fd_lblXNote);
		txtXNote = new WText(comXNote, SWT.BORDER, "", "备注");
		txtXNote.setTextLimit(256);
		FormData fd_txtXNote = new FormData();
		fd_txtXNote.left = new FormAttachment(lblXNote, 4);
		fd_txtXNote.top = new FormAttachment(comXNote, 2);
		fd_txtXNote.right = new FormAttachment(100, -8);
		fd_txtXNote.bottom = new FormAttachment(txtXNote, 30);
		txtXNote.setLayoutData(fd_txtXNote);
	
		//mainCom.pack();
		
		
		//this.texts.add(txtXName);
		this.texts.add(txtXNote);
		this.texts.add(txtXHint);
		this.texts.add(txtXDesc);
		this.initBtnStatus(true);
		this.initTextStatus(true);
		
		
		show();
		
		//self.tblParaTable.show("tblParaTable", "", null);
		//self.pack();
		//self.tblParaTable.setSize(self.getSize().x - 10, 300);
		//self.pack();
		this.refreshParas();
		this.refreshCols();
		
//		int ok = this.type & 1;
//		if(ok > 0) {
//			comParaTable.setEnabled(true);
//		}else {
//			comParaTable.setEnabled(false);
//		}
//		ok = this.type & 2;
//		if(ok > 0) {
//			comColTable.setEnabled(true);
//		}else {
//			comColTable.setEnabled(false);
//		}
		ShellUtil.center(self);
	}
	
	//protected abstract BaseDetailEditShell<? extends BaseDetailEditShell<?, E>> getEditShell(int dID);
	
	
	
	
	protected void refreshParas() {
		int ok = this.type & 1;
		if(ok > 0) {
			self.tblParaTable.show("tblParaTable", "", this.paraService.list(self.sID));
		}	
		self.pack();
		self.tblParaTable.setSize(self.getSize().x - 20, 300);
		self.pack();
	}
	protected void refreshCols() {
		int ok = this.type & 2;
		
		if(ok > 0) {
			self.tblColTable.show("tblColTable", "", this.colService.list(self.sID));
		}
		self.pack();
		self.tblColTable.setSize(self.getSize().x - 20, 300);
		//comColTable.pack();
		//comColTable.setSize(self.tblColTable.getSize());
		self.pack();
	}

	
	
	
	@Override
	protected boolean checkModelChange() {
		boolean rs = false;
		if(model != null) {
			rs = !model.getStr("xHint").equals(txtXHint.getText())
					|| !model.getStr("xNote").equals(txtXNote.getText())
					|| !model.getStr("xDesc").equals(txtXDesc.getText());
		}else {
			rs = !txtXHint.getText().equals("") && !txtXDesc.getText().equals("") && !txtXNote.getText().equals("");
		}
		return rs;
	}
	
	protected abstract int getTypeID();
	
	@Override
	protected boolean save() {
		// 校验
		if(sID <= 0 || model == null) {
			model = new YTObject();	// 不用预先指定ID, 保存时会自动生成
		}
		model.setXName(this.txtXName.getText()).setXNote(this.txtXNote.getText()).setXHint(txtXHint.getText()).setXDesc(txtXDesc.getText());
		if(model.getSID() == 0) {
			model.setROwnerID(dda.user.getSID()).setRAuthorID(dda.user.getSID())
			.setRMenderID(0).setRCheckerID(0).setRAuditorID(0).setSStatus(2).setRTypeID(this.getTypeID());
		}
		//masterService.saveOrUpdate(d);
		if(this.sID > 0) {
			return masterService.update(model);
		}else {
			return masterService.save(model);
		}
	}
	
	
	
	@Override
	protected void show() {
		//txtSID.removeModifyListener(modifyListener);
		//txtXName.removeModifyListener(modifyListener);
		txtXNote.removeModifyListener(modifyListener);
		txtXHint.removeModifyListener(modifyListener);
		txtXDesc.removeModifyListener(modifyListener);
		txtXName.setText(this.xName);
		if(model != null) {
			//txtSID.setText(model.getStr("sID"));
			txtXHint.setText(model.getStr("xHint"));
			txtXDesc.setText(model.getStr("xDesc"));
			txtXNote.setText(model.getStr("xNote"));
		
		}else {
			//txtSID.setText(String.valueOf(DDA.getInstance().getIDService.exec("yp_getid", 0, "yt_dic", 1)));
			//txtXName.setText("");
			txtXNote.setText("");
			txtXHint.setText("");
			txtXDesc.setText("");
		}
		//txtSID.addModifyListener(modifyListener);
		//txtXName.addModifyListener(modifyListener);
		txtXNote.addModifyListener(modifyListener);
		txtXHint.addModifyListener(modifyListener);
		txtXDesc.addModifyListener(modifyListener);
	}
	
	
	
	@Override
	protected void initBtnStatus(boolean isInit) {
		super.initBtnStatus(isInit);
		boolean enable = false;
		if(this.model != null && this.model.getSStatus().getSID() != 2 && self.btnEdit.getEnabled()) {
			enable = false;
		}else {
			enable = true;
		}
		if(this.model == null || this.model.getSStatus().getSID() != 2) {
			enable = false;
		}
		// 被父类调用时会报错!
		if(self.btnParaAdd != null) {
			int ok = this.type & 1;
			boolean paraEnable = false;
			if(ok > 0) {
				paraEnable = enable;
			}else {
				paraEnable = false;
			}
			self.btnParaClear.setEnabled(paraEnable);
			self.btnParaAdd.setEnabled(false);
			self.btnParaDel.setEnabled(paraEnable);
			self.btnParaRefresh.setEnabled(paraEnable);
			self.btnParaGet.setEnabled(paraEnable);
			self.btnParaAutoname.setEnabled(paraEnable);
			boolean colEnable = false;
			ok = this.type & 2;
			if(ok > 0) {
				colEnable = enable;
			}else {
				colEnable = false;
			}
			self.btnColClear.setEnabled(colEnable);
			self.btnColAdd.setEnabled(false);
			self.btnColDel.setEnabled(colEnable);
			self.btnColRefresh.setEnabled(colEnable);
			self.btnColGet.setEnabled(colEnable);
			self.btnColAutoname.setEnabled(colEnable);
		}
		
		this.btnAdd.setEnabled(false);//这个新增按钮永远不能用!只能从列表里进来!
		
	}
	
	
	
	protected ParaEditShell getParaEditShell(int mID, int dID){
		return new ParaEditShell<>(mID, dID, dda.objectParaService);
	}
	protected ColEditShell getColEditShell(int mID, int dID){
		return new ColEditShell(mID, dID, dda.objectColService);
	}
	protected abstract void getParas();
	protected abstract void getCols();
}

